package com.example.csvphonetoregion;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


/**
 * 从redis中读数据来获取手机号信息
 */
@Component
public class PhoneInfoByCsvUtil {

    private final static String PHONE_REGION_KEY = "country_phone_region_info";

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 根据手机号获取手机信息
     *
     * @param phone 手机号
     * @return 手机信息
     */
    public RegionVO getPhoneInfo(String phone) {
        String prefix = StringUtils.substring(phone, 0, 7);
        return (RegionVO) redisTemplate.opsForHash().get(PHONE_REGION_KEY, prefix);
    }

}
